package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import javax.swing.*;

import conference.Conference;
import conference.User;

/**
 * Creates a Choose a role panel.
 * @author Allison Graham
 *
 */
public class ChooseRolePanel extends Observable{
	private static final String TITLE = "Choose a Role";
	private JPanel myPanel;
	private JComboBox myCBox;
	private User myUser;
	private User[] myUsersRoles;
	private Conference myConference;
	private String myRole;
	 
    public ChooseRolePanel(final User theUser, final Conference theConference) {
    	myUser = theUser;
    	myConference = theConference;
    	myUsersRoles = myConference.getRolesByUser(myUser);
    	myPanel = new JPanel(getBorderLayout());  
    	
    	//TODO - checkUsers();
        
    	setUpContents();
        myPanel.setOpaque(true); //content panes must be opaque      
    }
    
    public JPanel getPanel() {
    	return myPanel;
    }
    
    public String getUserRole() {
    	return myRole;
    }
    
    private static BorderLayout getBorderLayout() {
    	BorderLayout myLayout = new BorderLayout();
    	myLayout.setHgap(ChooseConferencePanel.BORDER_SPACE);
    	return myLayout;
    }
    
    /**
     * Creates and sets up components of JPanel.
     */
    private void setUpContents() {    	
        //Create JLabel "Please select role: "
        JLabel selectLabel = new JLabel("Please Select Role: ");
 
        //Create the combo box
        ArrayList<String> myUserStrings = new ArrayList<String>(myUsersRoles.length);
        int[] removeArray = new int[myUsersRoles.length];
        for (int i = 0; i < myUsersRoles.length; i++) {
        	if (!myUserStrings.contains(myUsersRoles[i].getLastCommaFirst() + " - " + myUsersRoles[i].getClass().getSimpleName())) {
        		myUserStrings.add(myUsersRoles[i].getLastCommaFirst() + " - " + myUsersRoles[i].getClass().getSimpleName());
        		removeArray[i] = 0;
        	}
        	else {
        		removeArray[i] = 1;
        	}
        }
        
        User[] tempUserArray = new User[myUserStrings.size()];
        int index = 0;
        for (int i = 0; i < myUsersRoles.length; i++) {
        	if (removeArray[i] == 0) {
        		tempUserArray[index] = myUsersRoles[i];
        		System.out.println("Inside ChooseRGUI: User name is: " 
        				+ tempUserArray[index].getLastCommaFirst() 
        				+ " - " + tempUserArray[index].getClass().getSimpleName() 
        				+ ", User String is: " + myUserStrings.get(index));
        		index++;
        	}
        }
        myUsersRoles = tempUserArray;
        myUserStrings.trimToSize();
        
        myCBox = new JComboBox(myUserStrings.toArray(new String[myUserStrings.size()]));
        myCBox.addActionListener(
        	new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					myUser = myUsersRoles[myCBox.getSelectedIndex()];
					System.out.println("You chose: " + myUser.getClass().getSimpleName());
					myRole = myUser.getClass().getSimpleName();
					
				}
        	}
        );
        myCBox.setSelectedIndex(0);
        
        //Create Next Button
        JButton nextButton = new JButton("Next");
        nextButton.addActionListener(
        		new ActionListener(){
			public void actionPerformed(ActionEvent theEvent) {
				setChanged();
				notifyObservers(State.CHOOSE_ROLE);
				System.out.println("Selection is: " + myUser.getLastCommaFirst() + " - " + myUser.getClass().getName());
			}
        });
       
        //Lay out.
        myPanel.add(selectLabel, BorderLayout.WEST);
        myPanel.add(myCBox, BorderLayout.CENTER);
        JPanel southPanel = new JPanel();
        southPanel.add(GuiController.getSwitchConferenceButton());
        southPanel.add(GuiController.getSwitchUserButton());
        myPanel.add(southPanel, BorderLayout.SOUTH);
        myPanel.add(nextButton, BorderLayout.EAST);
        myPanel.setBorder(BorderFactory.createEmptyBorder(ChooseConferencePanel.BORDER_SPACE, ChooseConferencePanel.BORDER_SPACE, 
        		ChooseConferencePanel.BORDER_SPACE, ChooseConferencePanel.BORDER_SPACE));
    } 
    
    public User getUser() {
    	return myUser;
    	
    }
    
	public String getTitle() {
		return TITLE;
	}
}
